//
//  VoteFormView.m
//  KOFY
//
//  Created by Warren Holybee on 12/1/10.
//  Copyright 2010 Warren Holybee. All rights reserved.
//






#import "VoteFormView.h"
#import "defines.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"


@implementation VoteFormView
@synthesize question = _question;
@synthesize wheel;

-(IBAction)back: (id) sender {
	
	[[self parentViewController] dismissModalViewControllerAnimated:YES];
}

-(IBAction)vote: (id) sender {
	if (row != 99) {
		
	
		NSString *q = self.question.question;
		NSString *a = [self.question.answers objectAtIndex:row];
		emailText = [NSString stringWithFormat:@"A Vote has been cast:\n  %@\n  %@",q,a];
		self.send;

		} else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Opps !" 
														message:@"You didn't select an answer." 
													   delegate:self 
											  cancelButtonTitle:@"OK" 
											  otherButtonTitles:nil]; 
		[alert show];
		[alert release];	
	}

}
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	questionLabel.text = self.question.question;
	row = 99;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	
	return [_question.answers count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.textLabel.text = [self.question.answers objectAtIndex:[indexPath row]];
	
    return cell;
}


/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source.
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	row = [indexPath row];
	NSLog(@"row = %d",row);
	
	// Navigation logic may go here. Create and push another view controller.
    /*
	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
	 // ...
	 // Pass the selected object to the new view controller.
	 [self.navigationController pushViewController:detailViewController animated:YES];
	 [detailViewController release];
	 */
}

#pragma mark Email Stuff

-(void)send {
	
	SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = SMTP_FROM;
    testMsg.toEmail = SMTP_TO;
    testMsg.relayHost = SMTP_SERVER;
    testMsg.requiresAuth = NO;
	testMsg.wantsSecure = NO;
	testMsg.subject = @"iPhone vote";
	testMsg.delegate = self;
	
	NSLog(@"email text = %@", emailText);
	
	NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
                               emailText,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
    testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
	self.view.backgroundColor=[UIColor grayColor];
	
	[self.wheel startAnimating];
	[testMsg send];
	
}

- (void)messageSent:(SKPSMTPMessage *)message
{
    [message release];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Thank You" 
													message:@"Your vote has been sent." 
												   delegate:self 
										  cancelButtonTitle:@"OK" 
										  otherButtonTitles:nil]; 
	[alert show];
	[alert release];
	[self.wheel stopAnimating];
	self.view.backgroundColor=[UIColor whiteColor];
	[self back:self];
	
    NSLog(@"delegate - vote sent");
}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    [message release];
		self.view.backgroundColor=[UIColor whiteColor];
	[self.wheel stopAnimating];
    
    NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]);
}


@end
